package com.lzxuni.modules.dataAcquisition;

import com.lzxuni.common.utils.JsonResult;
import com.lzxuni.common.utils.JsonResultUtil;
import com.lzxuni.common.utils.R;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;
import com.lzxuni.modules.productdisplay.service.ProductDisplayService;
import com.lzxuni.modules.system.entity.BaseArea;
import com.lzxuni.modules.system.service.BaseAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Product")
public class ProductInformationController {

    @Autowired
    private ProductDisplayService productService;
    @Autowired
    private ChainService chainService;
    @Autowired
    private EnterpriseInfoService enterpriseInfoService;
    @Autowired
    private BaseAreaService areaService;
    @Autowired
    private FileEntityService fileEntityService;

    @RequestMapping("/getInformation")
    public Object getInformation(String productId) throws Exception {
        ProductDisplay productDisplay = productService.getById(productId);
        if (productDisplay != null) {
            Chain chain = chainService.getById(productDisplay.getTraceabilityChain());
            productDisplay.setChainName(chain == null ? "" : chain.getChainName());
            EnterpriseInfo enterpriseInfo = enterpriseInfoService.getById(productDisplay.getAffiliatedEnterprises());
            productDisplay.setEnterpriseName(enterpriseInfo == null ? "" : enterpriseInfo.getEnterpriseName());

            BaseArea area1 = areaService.getById(productDisplay.getProvinceId());
            productDisplay.setProvince(area1 == null ? "" : area1.getAreaName());
            BaseArea area2 = areaService.getById(productDisplay.getCityId());
            productDisplay.setCity(area2 == null ? "" : area2.getAreaName());
            BaseArea area3 = areaService.getById(productDisplay.getCountyId());
            productDisplay.setCounty(area3 == null ? "" : area3.getAreaName());
            BaseArea area4 = areaService.getById(productDisplay.getTownId());
            productDisplay.setTown(area4 == null ? "" : area4.getAreaName());
            BaseArea area5 = areaService.getById(productDisplay.getVillageId());
            productDisplay.setVillage(area5 == null ? "" : area5.getAreaName());

            FileEntity fileEntity = new FileEntity();
            fileEntity.setYwId(productDisplay.getYwId());
            fileEntity.setYwType("demopic");
            List<FileEntity> entities = fileEntityService.queryListByFileEntity(fileEntity);
            productDisplay.setFileEntities(entities);
            fileEntity.setYwType("demopic1");
            List<FileEntity> entities1 = fileEntityService.queryListByFileEntity(fileEntity);
            productDisplay.setFileEntities1(entities1);
        }
        return R.ok().put("data", productDisplay);
    }
}
